﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zebra.WinFramework.ApplicationIdle;
using Zebra.Utility.SysEnvironment;
using Zebra.Utility;
using System.Threading;
using System.Globalization;

namespace Zebra.WinFramework
{
    public partial class MainUCForm : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public static WarningForm warningForm = null;

        public MainUCForm()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(WarningForm.CultureInfo);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(WarningForm.CultureInfo); // what is this?

            InitializeComponent();
            this.Text += " V" + SysEnvironment.GetVersion().ToString();

            //Handling application idle
            StartApplicationIdleChecking();
        }

        #region System Idle Handling
        private void StartApplicationIdleChecking()
        {
            //http://www.codeproject.com/KB/miscctrl/Application_Idle.aspx
            applicationIdle.Started += new EventHandler(applicationIdle_Started);
            applicationIdle.TickAsync += new EventHandler<TickEventArgs>(applicationIdle_TickAsync);
            applicationIdle.Stopped += new EventHandler(applicationIdle_Stopped);
            applicationIdle.Activity += new EventHandler<ActivityEventArgs>(applicationIdle_Activity);
            applicationIdle.WarnAsync += new EventHandler(applicationIdle_WarnAsync);
            applicationIdle.IdleAsync += new EventHandler(applicationIdle_IdleAsync);
        }

        private void applicationIdle_Started(object sender, EventArgs e)
        {
        }

        private void applicationIdle_TickAsync(object sender, TickEventArgs e)
        {
            BeginInvoke(new MethodInvoker(
                            delegate() { applicationIdle_Tick(sender, e); })
                );
        }

        private void applicationIdle_Tick(object sender, TickEventArgs e)
        {
            if (e.IsWarnPeriod)
            {
                //ShowWarningForm
                if (warningForm == null)
                {
                    warningForm = new WarningForm();
                    warningForm.StartPosition = FormStartPosition.CenterScreen;
                    warningForm.Show();
                }
            }
            else
            {
            }
        }

        private void applicationIdle_Stopped(object sender, EventArgs e)
        {
        }

        private void applicationIdle_Activity(object sender, ActivityEventArgs e)
        {
        }

        private void applicationIdle_WarnAsync(object sender, EventArgs e)
        {
            //Console.Beep();
        }

        private void applicationIdle_IdleAsync(object sender, EventArgs e)
        {
            BeginInvoke(new MethodInvoker(
                            delegate() { applicationIdle_Idle(sender, e); })
                );
        }

        private void applicationIdle_Idle(object sender, EventArgs e)
        {
        }
        private void MainUCForm_Activated(object sender, EventArgs e)
        {
            //CloseWarningFrom
            if (warningForm != null)
            {
                warningForm.Close();
                warningForm = null;
            }
        }
        #endregion        

        private void MainUCForm_Load(object sender, EventArgs e)
        {
            #region Application Idle Time Setting
            applicationIdle.IdleTime = WarningForm.IdleTime; //new TimeSpan(hours, minutes, seconds)
            applicationIdle.WarnTime = WarningForm.WarningTime; //new TimeSpan(hours, minutes, seconds)
            applicationIdle.Start();

            #endregion
        }
        private void buttonSpecLeftRight_Click(object sender, EventArgs e)
        {
            kryptonHeaderGroupLeft.Visible = false;
            HeaderExpandMenu.Visible = true;
        }
        private void HeaderExpandMenu_Click(object sender, EventArgs e)
        {
            buttonSpecLeftRight.PerformClick();
            kryptonHeaderGroupLeft.Visible = true;
            HeaderExpandMenu.Visible = false;
        }
        private void headerTopButtonSpec_Click(object sender, EventArgs e)
        {
          //  headerTop.
        }
    }
}
